// export default withRouter(Login)
import React, { Component } from 'react'
import axios from '../utils/request'
import '../hotstop.css'
// import axios from 'axios'

export default class Login extends Component {
    //获取输入内容
    matter = (value, key) => {
        let { ruleFrom } = this.state
        ruleFrom[key] = value
        this.setState = {
            ruleFrom
        }
    }

    //登录
    login = (ruleFrom) => {
        axios({
            url: '/pc/login',
            method: 'post',
            data: {
                username: ruleFrom.username,
                password: ruleFrom.password,
            }
        }).then(res => {
            if (res.data.code === 1) {
                console.log(111);
                sessionStorage.setItem('token', res.data.token)
                sessionStorage.setItem('type', res.data.datas[0].type)
                sessionStorage.setItem('username', res.data.datas[0].username)
                sessionStorage.setItem('img', res.data.datas[0].img)
                //跳转
                this.props.history.push('/home/hotstop')
                alert('登录成功')
            } else {
                alert('失败了');
            }
        })
    }

    state = {
        ruleFrom: {}
    }
    render() {
        const { ruleFrom } = this.state
        return (
            <div className='login'>
                <div className="login_int">
                    <p>用户名:&ensp;<input type="text" value={ruleFrom.username} onChange={(e) => this.matter(e.target.value, 'username')} /></p>
                    <p>&ensp;密&ensp;码:&ensp;<input type="text" value={ruleFrom.password} onChange={(e) => this.matter(e.target.value, 'password')} /></p>
                    <p>&emsp;&emsp;<button onClick={() => this.login(ruleFrom)}>登录</button><button>注册</button></p>
                </div>
            </div>
        )
    }
}